$(function () {



	function _operation_field_formatter(cellValue , options , rowData){
		var mode = 'view';
		var buttons = [];
		var id = rowData['uid'];
		buttons.push('<div class="grid-operation-button">')

		if(mode == 'view') {
			buttons.push(str_format('<a class="label label-info" target="_blank" href="live_detail.html?uid={0}">查看</a>',rowData['uid']));
		}
		if(mode == 'modify') {
			buttons.push(str_format('<a class="label label-info" onclick="T.vm.update(' + id + ',event)" href="javascript:;">修改</a>', rowData['uid']));
		}
		buttons.push('</div>');
		return buttons.join("&nbsp;")
	}

	function formatter_live_state(cellvalue, options, row) {
		if(parseInt(cellvalue)<0){
			return '<span class="label label-danger">禁播</span>'
		}else if(parseInt(cellvalue)>0){
			return '<span class="label label-success">直播中</span>';
		}else{
			return '<span class="label label-info">未直播</span>';
		}
	}

    $("#jqGrid").jqGrid({
        url: baseURL + 'live/liveroom/list',
        datatype: "json",
        colModel: [			
			{ label: '主播信息', name: 'uid', index: 'uid', width: 50, key: true ,formatter : get_grid_column_formatter('live/liveroom/uid')},
			{ label: '当前场次', name: 'liveRoundId', index: 'live_round_id', width: 80 ,formatter : get_grid_column_formatter('live/liveroom/liveRoundId')},
			//{ label: '推荐级别', name: 'rank', index: 'rank', width: 80 ,formatter : get_grid_column_formatter('live/liveroom/rank')},
			//{ label: '标题', name: 'title', index: 'title', width: 80 ,formatter : get_grid_column_formatter('live/liveroom/title')},
			//{ label: '封面', name: 'coverImage', width: 80 ,formatter : get_grid_column_formatter('live/liveroom/coverImage')},
			//{ label: '密码', name: 'password', index: 'password', width: 80 ,formatter : get_grid_column_formatter('live/liveroom/password')},
			{ label: '人气', name: 'popCount', index: 'pop_count', width: 80 ,formatter : get_grid_column_formatter('live/liveroom/popCount')}, 			
			{ label: '累计礼物数', name: 'giftCount', index: 'gift_count', width: 80 ,formatter : get_grid_column_formatter('live/liveroom/giftCount')}, 			
			{ label: '在线人数', name: 'onlineCount', index: 'online_count', width: 80 ,formatter : get_grid_column_formatter('live/liveroom/onlineCount')}, 			
			{ label: '累计直播时间(s)', name: 'totalSeconds', index: 'total_seconds', width: 80 ,formatter : get_grid_column_formatter('live/liveroom/totalSeconds')},
			{ label: '状态', name: 'state', width: 80 ,formatter : formatter_live_state},
			{ label: '开始时间', name: 'startDate', index: 'start_date', width: 80 ,formatter : timestampForMS},
			{ label: '结束时间', name: 'endDate', index: 'end_date', width: 80 ,formatter : timestampForMS}
			,{ label: '操作', name: '_operation_field', index: '_operation_field', width:80 ,formatter:_operation_field_formatter}
        ],
		viewrecords: true,
		height: "100%",
        rowNum: 10,
		rowList : [10,30,50],
		rownumbers: false, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});
Vue.filter("date2datetime",function (value,format){
	return dateFormat(value,format);
});
Vue.filter("timestamp2datetime",function (value,format){
	return timestampFormat(value,format);
});
var vm  = T.vm = new Vue({
	el:'#webpage',
	data:{
		appearance : {
			showList: true,
			showControllBar : true,
			showSearchForm : false
		},
		q:{
			_keyword:''
		},
		title: null,
		liveRoom: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.appearance.showList = false;
			vm.title = "新增";
			vm.liveRoom = {};
		},
		update: function (targetId,event) {
			var uid = targetId || getSelectedRow();
			if(uid == null){
				return ;
			}
			vm.appearance.showList = false;
            vm.title = "修改";
            
            vm.getInfo(uid)
		},
		saveOrUpdate: function (event) {
			var url = vm.liveRoom.uid == null ? "live/liveroom/save" : "live/liveroom/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.liveRoom),
			    success: function(r){
			    	if(r.code > 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var uids = getSelectedRows();
			if(uids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "live/liveroom/delete",
                    contentType: "application/json",
				    data: JSON.stringify(uids),
				    success: function(r){
						if(r.code > 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(uid){
			$.get(baseURL + "live/liveroom/info/"+uid, function(r){
                vm.liveRoom = r.liveRoom;
            });
		},
		reload: function (event) {
			vm.appearance.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'_keyword': vm.q._keyword},
                page:page
            }).trigger("reloadGrid");
		}
	}
});